package com.nju.crawler.storage;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/**
 * @authorliyunfei
 * @date2022/11/11
 **/
public class FileUtil {

    public static void saveToFile(ArrayList<ArrayList<String>> data, String path, String type){
        try {
            //追加写
            BufferedWriter out =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path,true),"UTF-8"));
            for (ArrayList<String> row : data) {
                if(row == null || row.isEmpty())
                    continue;
                for (String s : row) {
                    if(s==null){
                        out.write("null");
                        continue;
                    }
                    out.write(delQuota(s));
                    out.write(",");
                }
                out.newLine();
            }
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String delQuota(String str){
        String result = str;
        String[] strQuota = { "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?" };
        for (String s : strQuota) {
            if (result.contains(s))
                result = result.replace(s, "");
        }
        return result;
    }

}
